<?php
namespace Admin\Controller;

class GoodsController extends CommonController{
	public function showAttr(){
		//ajax请求方法
		$type_id = intval(I("post.type_id"));
		if($type_id <= 0){
			echo "没有数据";exit;
		}
		$data = D("attribute")->where("type_id=".$type_id)->select();
		foreach ($data as $key => $value) {
			if($value["attr_input_type"] == 2){
				$data[$key]["attr_value"] = explode(",", $value["attr_value"]);
			}
		}
		$this->assign("data",$data);
		$this->display();
	}
	public function add(){
		if(IS_GET){
			//显示添加页面
			$Model = D("Category");
			$cate_data = $Model->getCateTree();
			$this->assign("cate_data",$cate_data);
			//商品属性
			$type = D("type")->select();
			$this->assign("type",$type);
			$this->display();
		}else{
			//处理添加
			$Model = D("Goods");
			$data = $Model->create();
			if(!$data){
				$this->error($Model->getError());
			}
			$insertId = $Model->add($data);
			if(!$insertId){
				$this->error($Model->getError());
			}
			$this->success("商品添加成功");
		}
	}
	public function index(){
		//显示出商品详细
		$Model = D("Goods");
		$data = $Model->listData();
		$this->assign("data",$data);
		//显示出分类列表
		$cate = D("Category")->getCateTree();
		$this->assign("cate",$cate);
		$this->display();
	}
	public function dels(){
		//伪删除
		$goods_id = intval(I("get.id"));
		if(!empty($goods_id) && $goods_id<=0){
			$this->error("参数错误",U("goods/index"));
		}
		$Model = D("goods");
		$res = $Model->setState($goods_id);
		if(!$res){
			$this->error("删除失败",U("goods/index"));
		}
		$this->success("删除成功",U("goods/index"));
	}
	public function edit(){
		if(IS_GET){
			$id = intval(I("get.id"));
			if($id<=0){
				$this->error("参数错误");
			}
			$Model = D("Goods");
			$info = $Model->findOneById($id);
			//分类获取
			$CateModel = D("Category");
			$cate = $CateModel->getCateTree();
			//扩展分类
			$goods_cate = M("GoodsCate")->where("goods_id=".$id)->select();
			//没有扩展分类的话手动追加扩展分类
			if(!$goods_cate){
				$goods_cate = [
					"msg" => "no data"
				];
			}
			$this->assign("goods_cate",$goods_cate);
			$this->assign("cate_data",$cate);
			//转换图片
			$info["goods_body"] = htmlspecialchars_decode($info["goods_body"]);
			$this->assign("info",$info);
			$this->display();
		}else{
			$Model = D("Goods");
			$data = $Model->create();
			if(!$data){
				$this->error($Model->getError());
			}

			$res = $Model->update($data);
			if(!$res){
				$this->error($Model->getError());
			}
			$this->success("修改成功",U("goods/index"));
		}
	}
	public function trash(){
		//回收站显示
		$Model = D("Goods");
		$data = $Model->listData(1);
		$this->assign("data",$data);
		//显示出分类列表
		$cate = D("Category")->getCateTree();
		$this->assign("cate",$cate);
		$this->display();
	}
	public function recover(){
		//商品还原
		$id = intval(I("get.id"));
		if($id<=0){
			$this->error("参数错误");
		}
		$res = D("Goods")->setState($id,0);
		if($res == false){
			$this->error("还原失败");
		}
		$this->success("还原成功",U("Goods/trash"));
	}
	public function remove(){
		//商品彻底删除
		$id = intval(I("get.id"));
		if($id<=0){
			$this->error("参数错误");
		}
		//删除图片
		$Model = D("Goods");
		$img = $Model->findOneById($id);

		unlink($img["goods_img"]);
		unlink($img["goods_thumb"]);
		//删除数据
		$res = $Model->dels($id);
		if($res == false){
			$this->error("删除失败");
		}
		$this->success("删除成功",U("Goods/trash"));
	}
}

?>